﻿using Microsoft.AspNetCore.Http;
using OpenMms.Common.Auth;
using OpenMms.Common.Cathe;
using OpenMms.Common.Log;
using OpenMms.Common.Session;
using OpenMms.Domain.Interface;
using OpenMms.Domain.Interface.User;
using OpenMms.Domain.Models;
using OpenMms.Domain.ReponseModels;
using OpenMms.Domain.RequestModels;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;

namespace OpenMms.Domain.BLL.User
{
    public class ModifyInfoService: IModifyInfoService
    {
        #region 初始化数据
        /// <summary>
        /// 数据访问
        /// </summary>
        private IBLLConstruct<Models.UserInforMations> _repository;
        private ICacheContext _cache;
        IHttpContextAccessor _httpContextAccessor;
        private IBLLConstruct<UserInforMations> _repositoryRead;
        public ModifyInfoService(IBLLConstruct<UserInforMations> repository,IAuth auth,
            ICacheContext cache, IHttpContextAccessor httpContextAccessor,
        IBLLConstruct<UserInforMations> repositoryRead)
        {
            this._cache = cache;
            this._httpContextAccessor = httpContextAccessor;
            this._repository = repository;
            this._repositoryRead = repositoryRead;
        }
        #endregion

        #region 修改资料
        /// <summary>
        /// 修改资料
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async Task<Response> ModifyInfo(ModifyInfoReq req)
        {
            Response result = new Response();
            //判断二级密码
            if (! await CheckSecondPwd(req))
            {
                result.Code = 202;
                result.Message = "二级密码输入错误!";
                return result;
            }
            //修改资料
            MyLog.Log(req, "User", "修改资料");
            await Modify(req);
            result.Code = 200;
            result.Message = "资料修改成功";
            return result;
        }
        #endregion

        #region 判断二级密码
        //判断二级密码
        private async Task<bool> CheckSecondPwd(ModifyInfoReq req)
        {
            var result = await _repositoryRead.CountAsync
                (u => u.UserCode == req.Code && u.UserSecondPassWord == req.UserSecondPassWord);
            if (result <=0)
            {
                return false;
            }
            return true;
        }
        #endregion

        #region 修改资料操作
        //修改资料操作
        private async Task Modify(ModifyInfoReq req)
        {
          var result =  await this._repository.UpdateAsync(u => u.UserCode == req.Code, u => new Models.UserInforMations()
            {
                UserName = req.UserName,
                IdNumber = req.IdNum,
                BankCode = req.BankCode,
                BankName = req.BankName,
                CardHolder = req.CardHolder,
                Phone = req.UserPhone
            });
            //更新缓存
            string token =_httpContextAccessor.HttpContext.Request.Cookies["Token"];
            if (!string.IsNullOrEmpty(token))
            {
                var session = _cache.Get<UserAuthSession>(token);
                session.Name = req.UserName;
                _cache.Set<UserAuthSession>(token, session, DateTime.Now.AddDays(10));
            }
        }
        #endregion
    }
}
